﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.Foundation.Metadata;
using Windows.UI;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace bgp8.Controls
{

    #region eventArgs
    public class GamePieceEventArgs : RoutedEventArgs
    {
        internal GamePieceEventArgs(GamePiece selected)
        {
            this.Piece = selected;
        }
        public GamePiece Piece { get; set; }
    }
    #endregion

    public abstract class GamePiece : GestureObject
    {

        #region Events
        //define the signature for GamePieceSelected event handlers
        public delegate void GamePieceSelectedHandler(Object sender, GamePieceEventArgs e);
        
        //declare our GamePieceSelected event to use the signature we just defined
        public event GamePieceSelectedHandler GamePieceSelected;

        //make it a little easier to invoke our event.
        private void OnGamePieceSelected(GamePieceEventArgs e)
        {
            GamePieceSelected(this, e);
        }
        #endregion

        public GamePiece()
        {
            this.DefaultStyleKey = typeof(GamePiece);
            this.ManipulationMode = ManipulationModes.All;
            Selected = false;
            Loaded += onLoad;
            ContentMargin = 8;
            PointerPressed += GamePiece_PointerPressed;
            PointerReleased += GamePiece_PointerReleased;
        }

        public abstract GamePiece Clone();


        void GamePiece_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            ReleasePointerCapture(e.Pointer);
        }

        private void GamePiece_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            CapturePointer(e.Pointer);
        }

        public static DependencyProperty HighlightVisibilityProperty = 
            DependencyProperty.Register(
            "HighlightVisibility", 
            typeof(Visibility), 
            typeof(GamePiece), 
            null );

        public static DependencyProperty HighlightBrushProperty = 
            DependencyProperty.Register(
            "HighlightBrush",
            typeof(Brush),
            typeof(GamePiece),
            null);

        public static DependencyProperty ContentMarginProperty =
            DependencyProperty.Register(
            "ContentMargin",
            typeof(int),
            typeof(GamePiece),
            null);

        public void toss() 
        {
        }

        private void onLoad(object sender, RoutedEventArgs e)
        {
            ManipulationManager = new Manipulations.ManipulationManager(this, Parent as Canvas);
            ManipulationManager.OnFilterManipulation = Manipulations.ManipulationFilter.Contain;
            HighlightBrush = new SolidColorBrush(Colors.WhiteSmoke);
            HighlightBrush.Opacity = .4;
            Configure(false, true, true, true);
        }

        public Brush HighlightBrush
        {
            get { return (Brush)GetValue(HighlightBrushProperty); }
            set { SetValue(HighlightBrushProperty, value); }
        }
        public int ContentMargin
        {
            get { return (int)GetValue(ContentMarginProperty); }
            set { SetValue(ContentMarginProperty, value); }
        }

        public Visibility HighlightVisibility {
            get { return (Visibility)GetValue(HighlightVisibilityProperty); }
            set { SetValue(HighlightVisibilityProperty, value); }
        }

        private bool _selected;
        public bool Selected 
        {
            set 
            {
                _selected = value;
                if (Selected == true) 
                {
                    OnGamePieceSelected(new GamePieceEventArgs(this));
                }
                HighlightVisibility = Selected ? Visibility.Visible : Visibility.Collapsed;
            }

            get 
            {
                return _selected;
            }
        }


    }
}
